//
//  NewFeaturViewController.m
//  weibo-OC
//
//  Created by diyuyma on 16/3/18.
//  Copyright © 2016年 zhangkaiyun. All rights reserved.
//

#define imageCount 4
#import "NewFeaturViewController.h"
#import "NewFeatureCell.h"

@interface NewFeaturViewController ()

@end

@implementation NewFeaturViewController

static NSString * const reuseIdentifier = @"Cell";
//storyboard 会自动注册cell并且指定layout

//重写集合视图控制器的构造方法
/*
 1.让外界调用更加简单  不需要传参数
 2.layout 和视图的大小比较固定
 */

- (instancetype)init {
    // 实例化 layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 1.指定layout 的itemSize  cell在实例化的时候 就会使用这个size
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self = [super initWithCollectionViewLayout:layout];
    
    // 设置分页滚动
    self.collectionView.pagingEnabled = true;
    self.collectionView.showsHorizontalScrollIndicator = false;
    self.collectionView.bounces = false;
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerClass:[NewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.index = indexPath.item;
    return cell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    int page = scrollView.contentOffset.x / kScreenWidth;
    if (page == imageCount - 1) {
        //设置开始按钮的 hidden
        
        //开始动画效果
        NewFeatureCell *cell = [[NewFeatureCell alloc] init];
        if ([cell isKindOfClass:[NewFeatureCell class]]) {
            cell = self.collectionView.visibleCells.lastObject;
            [cell startAnimation];
        }
    }
}

@end
